﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using eduscrum.Helper;
using System.Linq;

namespace eduscrum.ViewModels
{
    public class ProjectVM : IValidatableObject
    {
        public int ProjectID { get; set; }

        [Required]
        [DisplayName("Name")]
        [StringLength(40)]
        public string Name { get; set; }

        [Required]
        [DisplayName("Start date")]
        [DataType(DataType.Date)]
        public DateTime StartDate { get; set; }
        
        [Required]
        [DisplayName("End date")]
        [DataType(DataType.Date)]
        public DateTime EndDate { get; set; }

        [Required]
        [DisplayName("Number of Phases")]
        [Range(1,10)]
        public int PhaseCount { get; set; }

        [Required]
        [DisplayName("Sprint Duration, Days")]
        [Range(1, 30)]
        public int SprintDuration { get; set; } // Whole days

        public bool Locked { get; set; }

        public virtual List<UserStoryVM> Backlog { get; set; }

        public List<PhaseVM> phases = new List<PhaseVM>();
        public virtual List<PhaseVM> Phases { 
            get
            {
                return phases.OrderBy(x => x.PhaseID).ToList();
            } 
            set
            {
                phases = value;
            } 
        }
        public virtual List<SprintVM> Sprints { get; set; }

        public IEnumerable<ValidationResult> 
            Validate(ValidationContext validationContext)
        {
            var StartDateField = new[] { "StartDate" };
            var EndDateField = new[] { "EndDate" };

            int numberOfYearsBack = 5;
            DateTime earliestStartDate = DateTime.Now.AddYears(numberOfYearsBack * -1); // project startdate may not be more than numberOfYearsBack years in the past

            int numberOfYearsInTheFuture = 5;
            DateTime latestEndDate = DateTime.Now.AddYears(numberOfYearsInTheFuture); // project enddate may not be more than numberOfYearsInTheFuture years into the future
            
            DateTime EndDateSprint = StartDate.AddDays(SprintDuration); // will be used to check if at least one sprint can fit between StartDate and EndDate 

            if (StartDate < earliestStartDate)
            {
                yield return new ValidationResult("The project start date cannot be more than " + numberOfYearsBack + " years in the past", StartDateField);
            }

            if (StartDate > latestEndDate)
            {
                yield return new ValidationResult("The project cannot be planned more than " + numberOfYearsInTheFuture + " years into the future", StartDateField);
            }

            if (EndDate > latestEndDate)
            {
                yield return new ValidationResult("The project cannot be planned more than " + numberOfYearsInTheFuture + " years into the future", StartDateField);
            }            
            
            if (EndDate <= StartDate)
            {
                yield return new ValidationResult("The project end date cannot be before or on the start date", EndDateField);
            }

            if (EndDate <= EndDateSprint)
            {
                yield return new ValidationResult("With the present settings there is no room for a Sprint between Start Date and End Date", EndDateField);
            }
        }
    }
}